/**
  ******************************************************************************
  * File Name          : io.h
  * Description        : This file contains all the functions prototypes for 
  *                      the io  
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __io_bsp_H
#define __io_bsp_H

/* Includes ------------------------------------------------------------------*/
#include "gd32f4xx.h"
#include <stdio.h>

#define VFD_VER                    720

#ifndef COLLECTOR_USE_TYPE
	#define COLLECTOR_USE_NTC          1
	#define COLLECTOR_USE_LORA         0
	#define COLLECTOR_USE_TYPE         COLLECTOR_USE_NTC
#endif

#if((COLLECTOR_USE_TYPE != COLLECTOR_USE_NTC)&&(COLLECTOR_USE_TYPE != COLLECTOR_USE_LORA))
#error "Collector define error!!"
#endif

#define ERROR_PART1                0x20u
#define ERROR_PART2                0x40u
#define ERROR_PART3                0x60u
#define ERROR_PART4                0x80u
#define ERROR_PART5                0xA0u
#define ERROR_PART6                0xC0u
#define ERROR_PART7                0xE0u

#define TTY_RXBUF_MAXLEN           40

/*** Process control Timer ***/
#define CAT1_UART_IRQ_PRIORITY        7
#define CAT1_IO_IRQ_PRIORITY          8
#define LORA_UART_IRQ_PRIORITY        9
#define LORA_TIMER_IRQ_PRIORITY       10
#define LORA_IO_IRQ_PRIORITY          10
#define NTC_DMA_IRQ_PRIORITY          10
#define NTC_ADC_IRQ_PRIORITY          10
#define ROUTE485_UART_IRQ_PRIORITY    11
#define CTR_TC_TIMER_IRQ_PRIORITY     12
#define DWLCD_UART_IRQ_PRIORITY       13
#define DWLCD_TIMER_IRQ_PRIORITY      14
#define TTY_UART_IRQ_PRIORITY         15
#define TTY_TIMER_IRQ_PRIORITY        15
//#define VFD_UART_IRQ_PRIORITY        9
//#define VFD_TIMER_IRQ_PRIORITY       14


#define CTR_TC_TIMER                  TIMER5
#define CTR_TC_TIMER_CLOCK            RCU_TIMER5
#define CTR_TC_TIMER_IRQn             TIMER5_DAC_IRQn
#define CTR_TC_TIMER_IRQHandler       TIMER5_DAC_IRQHandler

#define LORA_TIMER                    TIMER6
#define LORA_TIMER_CLOCK              RCU_TIMER6
#define LORA_TIMER_IRQn               TIMER6_IRQn
#define LORA_TIMER_IRQHandler         TIMER6_IRQHandler

#define DWLCD_TIMER                   TIMER12
#define DWLCD_TIMER_CLOCK             RCU_TIMER12
#define DWLCD_TIMER_IRQn              TIMER7_UP_TIMER12_IRQn
#define DWLCD_TIMER_IRQHandler        TIMER7_UP_TIMER12_IRQHandler

#define TTY_TIMER                     TIMER13
#define TTY_TIMER_CLOCK               RCU_TIMER13
#define TTY_TIMER_IRQn                TIMER7_TRG_CMT_TIMER13_IRQn
#define TTY_TIMER_IRQHandler          TIMER7_TRG_CMT_TIMER13_IRQHandler


#define KEY_NET_Pin                    GPIO_PIN_11
#define KEY_NET_GPIO_Port              GPIOA
#define KeyNet()                       (GPIO_ISTAT(KEY_NET_GPIO_Port) & (KEY_NET_Pin))

#define LED_ON                         0x00u
#define LED_OFF                        0x01u

#define LED_RUN_Toggle()               LED1_Toggle()
#define LED_RUN(x)                     LED1(x)

#define LED_LORA_COUNT(x)              LED4(x)
#define LED_LORA_COUNT_Toggle()        LED4_Toggle()
#define LED_LORA_COMM(x)               LED5(x)
#define LED_LORA_COMM_Toggle()         LED5_Toggle()

#define LED_NTC_GET(x)                 LED4(x)
#define LED_NTC_GET_Toggle()           LED4_Toggle()
#define LED_NEC_ERR(x)                 LED5(x)
#define LED_NEC_ERR_Toggle()           LED5_Toggle()

#define LED_MQTT_Connect(x)            LED3(x)
#define LED_MQTT_Connect_Toggle()      LED3_Toggle()

#define LED_MQTT_Data(x)               LED2(x)
#define LED_MQTT_Data_Toggle()         LED2_Toggle()


#define LED1_Pin                       GPIO_PIN_3
#define LED1_GPIO_Port                 GPIOB
#define LED1(x)                        x?(GPIO_BOP(LED1_GPIO_Port) = (uint32_t)LED1_Pin):(GPIO_BC(LED1_GPIO_Port) = (uint32_t)LED1_Pin)
#define LED1_Toggle()                  (GPIO_TG(LED1_GPIO_Port) = (uint32_t)LED1_Pin)

#define LED2_Pin                       GPIO_PIN_4
#define LED2_GPIO_Port                 GPIOB
#define LED2(x)                        x?(GPIO_BOP(LED2_GPIO_Port) = (uint32_t)LED2_Pin):(GPIO_BC(LED2_GPIO_Port) = (uint32_t)LED2_Pin)
#define LED2_Toggle()                  (GPIO_TG(LED2_GPIO_Port) = (uint32_t)LED2_Pin)

#define LED3_Pin                       GPIO_PIN_11
#define LED3_GPIO_Port                 GPIOC
#define LED3(x)                        x?(GPIO_BOP(LED3_GPIO_Port) = (uint32_t)LED3_Pin):(GPIO_BC(LED3_GPIO_Port) = (uint32_t)LED3_Pin)
#define LED3_Toggle()                  (GPIO_TG(LED3_GPIO_Port) = (uint32_t)LED3_Pin)

#define LED4_Pin                       GPIO_PIN_5
#define LED4_GPIO_Port                 GPIOB
#define LED4(x)                        x?(GPIO_BOP(LED4_GPIO_Port) = (uint32_t)LED4_Pin):(GPIO_BC(LED4_GPIO_Port) = (uint32_t)LED4_Pin)
#define LED4_Toggle()                  (GPIO_TG(LED4_GPIO_Port) = (uint32_t)LED4_Pin)

#define LED5_Pin                       GPIO_PIN_10
#define LED5_GPIO_Port                 GPIOC
#define LED5(x)                        x?(GPIO_BOP(LED5_GPIO_Port) = (uint32_t)LED5_Pin):(GPIO_BC(LED5_GPIO_Port) = (uint32_t)LED5_Pin)
#define LED5_Toggle()                  (GPIO_TG(LED5_GPIO_Port) = (uint32_t)LED5_Pin)


#define NTC_CN1_Pin                    GPIO_PIN_2
#define NTC_CN1_GPIO_Port              GPIOA
#define NTC_CN1_ADC_CHANNEL            ADC_CHANNEL_2

#define NTC_CN2_Pin                    GPIO_PIN_3
#define NTC_CN2_GPIO_Port              GPIOA
#define NTC_CN2_ADC_CHANNEL            ADC_CHANNEL_3

#define NTC_CN3_Pin                    GPIO_PIN_4
#define NTC_CN3_GPIO_Port              GPIOA
#define NTC_CN3_ADC_CHANNEL            ADC_CHANNEL_4

#define NTC_CN4_Pin                    GPIO_PIN_6
#define NTC_CN4_GPIO_Port              GPIOA
#define NTC_CN4_ADC_CHANNEL            ADC_CHANNEL_6

#define POW_ADC_Pin                    GPIO_PIN_7
#define POW_ADC_GPIO_Port              GPIOA
#define POW_ADC_ADC_CHANNEL            ADC_CHANNEL_7

#define RELAY_ON                         1u
#define RELAY_OFF                        0u

#define RELAY01_Pin                      GPIO_PIN_4
#define RELAY01_GPIO_Port                GPIOC
#define Relay01(x)                       x?(GPIO_BOP(RELAY01_GPIO_Port) = (uint32_t)RELAY01_Pin):(GPIO_BC(RELAY01_GPIO_Port) = (uint32_t)RELAY01_Pin)
#define Relay01_Read()                   (GPIO_ISTAT(RELAY01_GPIO_Port)&(RELAY01_Pin))

#define RELAY02_Pin                      GPIO_PIN_5
#define RELAY02_GPIO_Port                GPIOC
#define Relay02(x)                       x?(GPIO_BOP(RELAY02_GPIO_Port) = (uint32_t)RELAY02_Pin):(GPIO_BC(RELAY02_GPIO_Port) = (uint32_t)RELAY02_Pin)
#define Relay02_Read()                   (GPIO_ISTAT(RELAY02_GPIO_Port)&(RELAY02_Pin))

#define RELAY03_Pin                      GPIO_PIN_0
#define RELAY03_GPIO_Port                GPIOB
#define Relay03(x)                       x?(GPIO_BOP(RELAY03_GPIO_Port) = (uint32_t)RELAY03_Pin):(GPIO_BC(RELAY03_GPIO_Port) = (uint32_t)RELAY03_Pin)
#define Relay03_Read()                   (GPIO_ISTAT(RELAY03_GPIO_Port)&(RELAY03_Pin))

#define RELAY04_Pin                      GPIO_PIN_1
#define RELAY04_GPIO_Port                GPIOB
#define Relay04(x)                       x?(GPIO_BOP(RELAY04_GPIO_Port) = (uint32_t)RELAY04_Pin):(GPIO_BC(RELAY04_GPIO_Port) = (uint32_t)RELAY04_Pin)
#define Relay04_Read()                   (GPIO_ISTAT(RELAY04_GPIO_Port)&(RELAY04_Pin))

#define RELAY05_Pin                      GPIO_PIN_2
#define RELAY05_GPIO_Port                GPIOB
#define Relay05(x)                       x?(GPIO_BOP(RELAY05_GPIO_Port) = (uint32_t)RELAY05_Pin):(GPIO_BC(RELAY05_GPIO_Port) = (uint32_t)RELAY05_Pin)
#define Relay05_Read()                   (GPIO_ISTAT(RELAY05_GPIO_Port)&(RELAY05_Pin))

#define RELAY06_Pin                      GPIO_PIN_9
#define RELAY06_GPIO_Port                GPIOE
#define Relay06(x)                       x?(GPIO_BOP(RELAY06_GPIO_Port) = (uint32_t)RELAY06_Pin):(GPIO_BC(RELAY06_GPIO_Port) = (uint32_t)RELAY06_Pin)
#define Relay06_Read()                   (GPIO_ISTAT(RELAY06_GPIO_Port)&(RELAY06_Pin))

#define RELAY07_Pin                      GPIO_PIN_10
#define RELAY07_GPIO_Port                GPIOE
#define Relay07(x)                       x?(GPIO_BOP(RELAY07_GPIO_Port) = (uint32_t)RELAY07_Pin):(GPIO_BC(RELAY07_GPIO_Port) = (uint32_t)RELAY07_Pin)
#define Relay07_Read()                   (GPIO_ISTAT(RELAY07_GPIO_Port)&(RELAY07_Pin))

#define RELAY08_Pin                      GPIO_PIN_11
#define RELAY08_GPIO_Port                GPIOE
#define Relay08(x)                       x?(GPIO_BOP(RELAY08_GPIO_Port) = (uint32_t)RELAY08_Pin):(GPIO_BC(RELAY08_GPIO_Port) = (uint32_t)RELAY08_Pin)
#define Relay08_Read()                   (GPIO_ISTAT(RELAY08_GPIO_Port)&(RELAY08_Pin))

#define RELAY09_Pin                      GPIO_PIN_12
#define RELAY09_GPIO_Port                GPIOE
#define Relay09(x)                       x?(GPIO_BOP(RELAY09_GPIO_Port) = (uint32_t)RELAY09_Pin):(GPIO_BC(RELAY09_GPIO_Port) = (uint32_t)RELAY09_Pin)
#define Relay09_Read()                   (GPIO_ISTAT(RELAY09_GPIO_Port)&(RELAY09_Pin))

#define RELAY10_Pin                      GPIO_PIN_13
#define RELAY10_GPIO_Port                GPIOE
#define Relay10(x)                       x?(GPIO_BOP(RELAY10_GPIO_Port) = (uint32_t)RELAY10_Pin):(GPIO_BC(RELAY10_GPIO_Port) = (uint32_t)RELAY10_Pin)
#define Relay10_Read()                   (GPIO_ISTAT(RELAY10_GPIO_Port)&(RELAY10_Pin))

#define RELAY11_Pin                      GPIO_PIN_14
#define RELAY11_GPIO_Port                GPIOE
#define Relay11(x)                       x?(GPIO_BOP(RELAY11_GPIO_Port) = (uint32_t)RELAY11_Pin):(GPIO_BC(RELAY11_GPIO_Port) = (uint32_t)RELAY11_Pin)
#define Relay11_Read()                   (GPIO_ISTAT(RELAY11_GPIO_Port)&(RELAY11_Pin))

#define RELAY12_Pin                      GPIO_PIN_15
#define RELAY12_GPIO_Port                GPIOE
#define Relay12(x)                       x?(GPIO_BOP(RELAY12_GPIO_Port) = (uint32_t)RELAY12_Pin):(GPIO_BC(RELAY12_GPIO_Port) = (uint32_t)RELAY12_Pin)
#define Relay12_Read()                   (GPIO_ISTAT(RELAY12_GPIO_Port)&(RELAY12_Pin))


typedef struct
{
	const uint8_t collecter_type;
  const uint8_t routever;
  const char vfd_ver[4];
	const char ver[24];
	const char bdata[32];
}Sys_VersionType;

typedef struct
{
  uint8_t net_tx_pl;
  uint8_t net_tx_tp;
  uint8_t net_rx_pl;
  uint8_t net_rx_tp;
}Sys_DebugSwitchType;

typedef struct
{
	volatile uint8_t  year;  /* 0~99 */
	volatile uint8_t  month;
	volatile uint8_t  day;
	volatile uint8_t  hour;
	volatile uint8_t  minute;
	volatile uint8_t  second;
	volatile uint32_t allsec;
}Sys_DataTimeType;

extern volatile Sys_DebugSwitchType Sys_DebugSwitch;
extern const Sys_VersionType Sys_Version;

extern Sys_DataTimeType Sys_Runtime;
extern Sys_DataTimeType Sys_Adjtime;

extern void SysHal_Init(void);

extern void Systty_SendStr(const char *buf);
extern void Systty_SendBuf(const uint8_t *buf, uint32_t len);

extern void Sys_GetDataTime(Sys_DataTimeType *dt);
extern void Sys_SetDataTime(Sys_DataTimeType *dt);

extern void VFD_BspInit(void);
extern void VFD_Rs485Send(const uint8_t *buf, uint32_t len);

extern void RS485_2_BspInit(void);
extern void RS485_2_UartSendBuf(const uint8_t *buf, uint32_t len);


extern int day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end);

extern void ADC_Config(void);
extern uint8_t ADC_GetValue(void);

extern void RS485_2_BspInit(void);
extern void RS485_2_UartSendBuf(const uint8_t *buf, uint32_t len);

#define  SysDbg_net_tx_pl(...)    if(Sys_DebugSwitch.net_tx_pl != 0)_printf(__VA_ARGS__);
#define  SysDbg_net_tx_tp(...)    if(Sys_DebugSwitch.net_tx_tp != 0)_printf(__VA_ARGS__);
#define  SysDbg_net_rx_pl(...)    if(Sys_DebugSwitch.net_rx_pl != 0)_printf(__VA_ARGS__);
#define  SysDbg_net_rx_tp(...)    if(Sys_DebugSwitch.net_rx_tp != 0)_printf(__VA_ARGS__);

extern void rs485test(void);
/* debug macro */
#if (SYS_DEBUG_LEVEL > 0)
#define  SysUsrLog(...)    _printf(__VA_ARGS__);

#else
#define SysUsrLog(...)   
#endif 
                            
#if (SYS_DEBUG_LEVEL > 1)                         
#define  SysDbgLog(...)    _printf(__VA_ARGS__);//_printf("[D]:") ;
                           
#else
#define SysDbgLog(...)                         
#endif

#ifdef SYS_ERRLOG
#define  SysErrLog(...)    _printf("[E] '%s' '%s' '%d':\r\n", __FILE__, __FUNCTION__, __LINE__) ;\
                           _printf(__VA_ARGS__);
#else
#define SysErrLog(...)   
#endif 

#endif /* __io_bsp_H */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
